/*
 * This computer program is the confidential information and proprietary trade
 * secret of Apptonelabs, Inc. Possessions and use of this program must
 * conform strictly to the license agreement between the user and
 * Apptonelabs, Inc., and receipt or possession does not convey any rights
 * to divulge, reproduce, or allow others to use this program without specific
 * written authorization of Apptonelabs, Inc.
 * 
 * Copyright (c) 2012-2015 Apptonelabs, Inc. All Rights Reserved.
 */
package com.apptonelabs.vnc.decoder;

import java.awt.Graphics;
import java.io.IOException;

import com.apptonelabs.vnc.vphone.RfbInputStream;

//
// Class that used for decoding CopyRect encoded data.
//
public class CopyRectDecoder extends RawDecoder {

	final static int EncodingCopyRect = 1;

	public CopyRectDecoder(Graphics g, RfbInputStream is) {
		super(g, is);
	}

	public CopyRectDecoder(Graphics g, RfbInputStream is, int frameBufferW,
			int frameBufferH) {
		super(g, is, frameBufferW, frameBufferH);
	}

	//
	// Override handleRect method handle CopyRect
	//
	public void handleRect(int x, int y, int w, int h) throws IOException {

		//
		// Write encoding ID to record output stream
		//

		if (dos != null) {
			dos.writeInt(CopyRectDecoder.EncodingCopyRect);
		}

		int copyRectSrcX = rfbis.readU16();
		int copyRectSrcY = rfbis.readU16();

		// If the session is being recorded:
		if (dos != null) {
			dos.writeShort(copyRectSrcX);
			dos.writeShort(copyRectSrcY);
		}

		graphics.copyArea(copyRectSrcX, copyRectSrcY, w, h, x - copyRectSrcX, y
				- copyRectSrcY);
	}
}
